{	Problem: http://codechef.com/problems/BWALL/
 	Verdict: Accepted
}
const md=1000000007;
 
var
	n,i,k,ind,x:longint;
	a:array[0..40000] of longint;
	b:array[0..40000] of double;
	s,m:double;
	r:int64;
begin
read(n);
while n>0 do begin
	k:=1 shl n-1;
	for i:=1 to k do begin
		read(a[i]);
		b[i]:=ln(a[i]);
	end; 
	m:=-maxlongint;
	ind:=k;
	for i:=1 shl (n-1) to k do begin
		s:=b[i];
		x:=i shr 1;
		while x>0 do begin
			s:=s+b[x];
			x:=x shr 1;
		end;
		if s>m then begin
			m:=s;
			ind:=i;
		end;
	end;
	r:=a[ind];
	ind:=ind shr 1;
	while ind>0 do begin
		r:=(r*a[ind]) mod md;
		ind:=ind shr 1;
	end;
	writeln(r);
	read(n);
end;const md=1000000007;
 
var
	i,t,n,k,res:longint;
	r,c:array[0..3] of int64;
 
procedure ml(a,b:array of int64; var c:array of int64); 
begin
c[0]:=(a[0]*b[0]+a[1]*b[2]) mod md;
c[1]:=(a[0]*b[1]+a[1]*b[3]) mod md;
c[2]:=(a[2]*b[0]+a[3]*b[2]) mod md;
c[3]:=(a[2]*b[1]+a[3]*b[3]) mod md;
end;	
	
begin
read(t);
for i:=1 to t do begin
	read(n);
	r[0]:=1;
	r[1]:=0;
	r[2]:=0;
	r[3]:=1;
	c[0]:=2;
	c[1]:=2;
	c[2]:=1;
	c[3]:=0;
	k:=n;
	inc(n);
	while n>0 do begin
		if n and 1>0 then ml(r,c,r);
		ml(c,c,c);
		n:=n shr 1;		
	end;
	res:=(r[0]-r[1]+md) mod md;
	if k and 1>0 then res:=(res-1) mod md else res:=(res+1) mod md;
	writeln(res);
end;
end. 